package activity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.horo.myapplication.R;

import application.BasisApplication;
import tools.CustomProgressDialog;
import utils.texttools.SystemBarTintManager;

/**
 * activity 基类
 * Created by Risky on 15/10/28.
 */
public abstract class AbsBaseActivity extends AppCompatActivity {

    protected static final String TAG = AbsBaseActivity.class.getSimpleName();
    protected String tag = getClass().getSimpleName();
    private CustomProgressDialog dialog;

    /**
     * 简化findViewById()方法
     *
     * @param resId
     * @param <T>
     * @return
     */
    protected <T extends View> T findView(int resId) {
        T t = (T) findViewById(resId);
        return t;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        initPresenter();
        setContentView(getLayout());
        ActivityControl.addAty(tag, this);
        dialog = new CustomProgressDialog(this, "数据加载中...", R.anim.loding_better);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(getTitleColor());
        }
        initView();
        initData();
    }


    /**
     * 初始化Presenter
     */
    protected void initPresenter() {

    }


    /**
     * 返回值为所要加载的布局文件
     *
     * @return
     */
    protected abstract int getLayout();

    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 显示dialog
     */
    protected void showDialog() {
        dialog.show();
    }

    /**
     * 隐藏dialog
     */
    protected void dismissDialog() {
        dialog.dismiss();
    }

    /**
     * Toast一个数据加载失败
     */
    protected void toastError() {
        showCustomToast("数据加载失败*_*!", R.mipmap.link);
    }

    /**
     * 正常提示
     */
    protected void toastPrompt(String toast) {
        showCustomToast(toast, R.mipmap.wifi_device_selected);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityControl.removeAty(tag);
    }

    /**
     * 自定义Toast
     *
     * @param s
     * @param image
     */
    private void showCustomToast(String s, int image) {
        //获取LayoutInflater对象，该对象可以将布局文件转换成与之一致的view对象
        LayoutInflater inflater = getLayoutInflater();
        //将布局文件转换成相应的View对象
        View layout = inflater.inflate(R.layout.custome_toast,
                (ViewGroup) findViewById(R.id.toast_layout_root));
        //从layout中按照id查找imageView对象
        ImageView imageView = (ImageView) layout.findViewById(R.id.ivForToast);
        //设置ImageView的图片
        imageView.setBackgroundResource(image);
        //从layout中按照id查找TextView对象
        TextView textView = (TextView) layout.findViewById(R.id.tvForToast);
        //设置TextView的text内容
        textView.setText(s);
        //实例化一个Toast对象
        Toast toast = new Toast(BasisApplication.getContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setView(layout);
        toast.show();
    }

    /**
     * 任务栏颜色沉浸
     *
     * @param on
     */
    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }
}
